<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-10-11
 * Time: 10:27
 */

namespace App\Http\Controllers\Api\V1\Front\Wallet;


use App\Http\Controllers\Controller;
use App\Libs\httpUtil;
use App\Libs\WXBizDataCrypt;
use App\Models\AreaTotal;
use App\Models\DeviceSetting;
use App\Models\IntegrationRecord;
use App\Models\Order;
use App\Models\OrderDetail;
use App\Models\User;
use App\Models\UserIncomeLog;
use App\Models\UserWithdrawLog;
use App\Services\Front\AreaTotalService;
use App\Services\Front\IncomeService;
use App\Services\Wx\AliService;
use Illuminate\Http\Request;


class ScoreController extends Controller
{
    public function getScoreShow(Request $request)
    {
        $user_id = $request->USERID;
        $pageSize = $request->pageSize;
        $user = User::select('id', 'user_id', 'headimgurl', 'isZFB', 'user_name', 'level', 'zfbName', 'zfbNumber', 'income', 'totalIncome', 'todayTaskNum', 'totalWithdraw', 'totalSaleNum','integration')->where('user_id', $user_id)->first();
        $incomeLogList = UserIncomeLog::select('incomeLogId','price','integration','createTime')
            ->where('source',"TASK")->where('user_id',$user_id)
            ->orderBy("createTime","desc")
            ->paginate($pageSize)->toArray();
        foreach ($incomeLogList['data'] as &$value){
            $value['price'] = number_format($value['price'],2);
        }
        $user['incomeLogList'] = $incomeLogList['data'];
        $user['totalNum'] = $incomeLogList['total'];
        return $this->apiResponse($user);
    }

    // 积分详细
    public function integral(Request $request)
    {
        $user_id = $request->USERID;
        $pageSize = $request->pageSize;
        $timer = $request->timer;
        $sort = $request->sort;
        if($sort){
            $sort = ' asc ';
        }else{
            $sort = ' desc ';
        }
        $user = User::select('id', 'user_id', 'headimgurl', 'user_name', 'level', 'score', 'integration')->where('user_id', $user_id)->first();
        $user_id = $user['user_id']; // 用户id
        $integration = $user['integration']; // 奖励积分
        $time = date("Y-m-d",strtotime("- 3 MONTH"))." 00:00:00";
        $sql = IntegrationRecord::select('t1.integration_score','t1.event','t1.add_date','t1.type','t1.is_stop','t2.p_id','t2.p_name','t2.p_price','t2.num','t1.expiration_date')
            ->from((new IntegrationRecord())->getTable() . ' as t1')
            ->leftJoin((new OrderDetail())->getTable() . ' as t2','t1.order_id', '=', 't2.id')
            ->where('t1.user_id',$user_id);
        if($timer){
            $sql->where('t1.add_date','>=',$time);
        }
        $integrationRecordList = $sql->orderBy('t1.add_date',$sort)->paginate($pageSize)->toArray();
        $user['integrationRecordList'] = $integrationRecordList['data'];
        $user['totalNum'] = $integrationRecordList['total'];
        return $this->apiResponse($user);
    }
}